//
//  DetalisViewController.m
//  TrueLove
//
//  Created by dlios on 15-1-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetalisViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "DetalisWorkTeam.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "UIButton+WebCache.h"
@interface DetalisViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain) UIScrollView *scrollMain;
@property (nonatomic, retain) NSMutableDictionary *urlDic;
@property (nonatomic, retain) MBProgressHUD *progerss;
@property (nonatomic, retain) UIVisualEffectView *visual;
@property (nonatomic, retain) UIView *buyView;
@end

@implementation DetalisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"套餐";
    self.navigationController.navigationBar.translucent = NO;
    //[self createScrollViewMain];
    //[self createScrollViewPic];
    [self getURl];
    // 小菊花
    
    self.progerss = [[MBProgressHUD alloc] initWithView:self.view];
    self.progerss.mode = MBProgressHUDModeText;
    self.progerss.labelText = @"稍等";
    self.progerss.margin = 10 ;
    self.progerss.yOffset = 50;
    self.progerss.removeFromSuperViewOnHide = YES;
    [self.progerss show:YES];
    [self.view addSubview:self.progerss];
    [_progerss release];
    
}

- (void)getURl
{
    NSString *str1 = @"http://www.hunliji.com/v1/api/app/works/";
    NSString *str2 = self.IDName;
    NSString *str = [NSString stringWithFormat:@"%@%@",str1,str2];
   // NSLog(@"%@",str);
    
    self.urlDic = [NSMutableDictionary dictionary];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

    [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.urlDic = [responseObject objectForKey:@"work"];
        
       
        //NSLog(@"+++++++++++++%@",self.urlDic);
        [self createScrollViewPic];
        [self addImageView];
        [self createScrollViewMain];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    
    
}



- (void)createScrollViewPic
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    NSMutableArray *arr = [self.urlDic objectForKey:@"media_items"];
    NSLog(@"++++++++++%ld",arr.count);
    self.scrollView.contentSize = CGSizeMake(arr.count *self.view.frame.size.width, 0);
    
    self.scrollView.pagingEnabled = YES;
    self.scrollView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:243/255.0 alpha:1];
    [self.view addSubview:self.scrollView];
    self.scrollView.delegate = self;
    [self addImageView];
    [_scrollView release];
    
}

- (void)addImageView
{
    
    NSMutableArray *arr = [self.urlDic objectForKey:@"media_items"];
    NSLog(@"===========%ld",arr.count);
// NSString *str = [[[self.urlDic objectForKey:@"media_items"] objectAtIndex:0] objectForKey:@"width"];
//    NSInteger width = [str intValue];
//    NSLog(@"%ld",width);
    
    
    
    for (int i = 0; i < arr.count; i ++) {
        
        UIImageView *image = [[UIImageView alloc] init];
        image.frame = CGRectMake( 120 + i *self.scrollView.frame.size.width, 0, 140, 200);
        //image.backgroundColor = [UIColor blackColor];
        image.contentMode = UIViewContentModeScaleAspectFill;
        [self.scrollView addSubview:image];
        NSString *name = [[arr objectAtIndex:i] objectForKey:@"media_path"];
        NSURL *url = [NSURL URLWithString:name];
        [image setImageWithURL:url];
        
        
       
        [image release];
        
        }
    
}

- (void)createScrollViewMain
{
    self.scrollMain = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height - 200)];
    self.scrollMain.contentSize = CGSizeMake(0, 2000);
    [self.view addSubview:self.scrollMain];
    [_scrollMain release];
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, 2000)];
//    view.backgroundColor = [UIColor redColor];
//    [self.scrollMain addSubview:view];
//    [view release];
    
//    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
//    NSMutableArray *arr = [self.urlDic objectForKey:@"media_items"];
//    NSLog(@"++++++++++%ld",arr.count);
//    self.scrollView.contentSize = CGSizeMake(arr.count *self.view.frame.size.width, 0);
//    
//    
//    self.scrollView.pagingEnabled = YES;
//    self.scrollView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:243/255.0 alpha:1];
//    [self.view addSubview:self.scrollView];
//    self.scrollView.delegate = self;
//    //[self addImageView];
//    [_scrollView release];
    

    
    
    
    
    
    DetalisWorkTeam *detalisTeam = [[DetalisWorkTeam alloc] init];
    [detalisTeam setValuesForKeysWithDictionary:self.urlDic];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 250, 250, 30)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = detalisTeam.title;
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.scrollMain addSubview:titleLabel];
    [titleLabel release];
    
    UILabel *kindLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 290, 80, 30)];
    kindLabel.textAlignment = NSTextAlignmentCenter;
    NSString *str =  detalisTeam.kind;
    NSString *kind = [NSString stringWithFormat:@"#%@",str];
    kindLabel.text = kind;
    kindLabel.font = [UIFont boldSystemFontOfSize:15];
    kindLabel.textColor = [UIColor grayColor];
    [self.scrollMain addSubview:kindLabel];
    [kindLabel release];
    
    UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 320, 300, 200)];
    
    logoView.backgroundColor = [UIColor whiteColor];
    [self.scrollMain addSubview:logoView];
    NSString *logo = [detalisTeam.merchant objectForKey:@"logo_path"];
    NSURL *url = [NSURL URLWithString:logo];
    [logoView setImageWithURL:url];
     [self.progerss hide:YES];
    
    
    UILabel *decribe = [[UILabel alloc] initWithFrame:CGRectMake(120, 600, 100, 30)];
    decribe.text = @"套餐描述";
    decribe.font = [UIFont boldSystemFontOfSize:20];
    decribe.textAlignment = NSTextAlignmentCenter;
    [self.scrollMain addSubview:decribe];
    [decribe release];
    
    UILabel *describeLabel = [[UILabel alloc] init];
    
    describeLabel.backgroundColor = [UIColor whiteColor];
    describeLabel.numberOfLines = 0;
    
    describeLabel.text = detalisTeam.describe;
    
    
    describeLabel.frame = CGRectMake(20, 650, 330, 200);
    
    
    [self.scrollMain addSubview:describeLabel];
    [describeLabel release];
    // 购买须知
    UILabel *tellLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 890, 100, 30)];
    tellLabel.text = @"购买须知";
    tellLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.scrollMain addSubview:tellLabel];
    [tellLabel release];
    //购买须知内容
    
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 930, 320, 200)];
    noteLabel.text = detalisTeam.purchase_notes;
    [self.scrollMain addSubview:noteLabel];
    noteLabel.numberOfLines = 0;
    noteLabel.backgroundColor = [UIColor whiteColor];
    [noteLabel release];
    
    UIButton *buyButton  = [UIButton buttonWithType:UIButtonTypeSystem];
    buyButton.frame = CGRectMake(140, 1150, 100, 30);
    [buyButton setTitle:@"查看全部" forState:UIControlStateNormal];
    [buyButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [buyButton addTarget:self action:@selector(buyButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollMain addSubview:buyButton];

    //[self buyButton:buyButton];
    // 建立一个 在屏幕底下的label
    
    self.buyView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width, 250)];
    //self.buyView.backgroundColor = [UIColor redColor];
    self.buyView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.buyView];
    [_buyView release];
    
    UILabel *buyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.buyView.frame.size.width, self.buyView.frame.size.height)];
    buyLabel.text = detalisTeam.purchase_notes;
    buyLabel.numberOfLines = 0;
    [self.buyView addSubview:buyLabel];
    [buyLabel release];
    
    
}

- (void)buyButton:(UIButton *)button
{
    NSLog(@"点击");
    self.visual = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    self.visual.frame =self.view.bounds;
    self.visual.alpha = 0.5;
    [self.view addSubview:self.visual];
    [_visual release];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.buyView.frame = CGRectMake(0, 300, self.view.frame.size.width, 400);
        [self.view addSubview:self.buyView];
    } completion:^(BOOL finished) {
        
    }];
    
    
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸");
    self.visual.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        
        self.buyView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 400);
        [self.view addSubview:self.buyView];
        
    } completion:^(BOOL finished) {
    
    }];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
